package com.lindroy.androidanimation

import android.graphics.drawable.AnimationDrawable
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_frame_anim.*


/**
 * @author Lin
 * @date 2021/3/27
 * @function
 * @Description
 */
class FrameAnimActivity : AppCompatActivity() {
    private lateinit var animationDrawable: AnimationDrawable

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_frame_anim)
        //方式一：xml添加
//        ivFrame.setBackgroundResource(R.drawable.frame_animation)
//        animationDrawable = ivFrame.background as AnimationDrawable
        //方式二：代码中添加
        animationDrawable = AnimationDrawable()
        for (i in 1..11) {
            val id = resources.getIdentifier("android$i", "drawable", packageName)
            val drawable = resources.getDrawable(id)
            animationDrawable.addFrame(drawable, 100)
        }
        ivFrame.setImageDrawable(animationDrawable)
        btnStart.setOnClickListener {
            animationDrawable.start()
        }
        btnStop.setOnClickListener {
            animationDrawable.stop()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        //Activity销毁时关闭动画
        animationDrawable.stop()
    }
}